package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.SQuestion;
import com.zhentao.domain.SUpdateQuestion;
import com.zhentao.domain.dto.SUpdateQuestionDto;
import com.zhentao.mapper.SQuestionMapper;
import com.zhentao.result.Result;
import com.zhentao.service.SUpdateQuestionService;
import com.zhentao.mapper.SUpdateQuestionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 86150
* @description 针对表【s_update_question】的数据库操作Service实现
* @createDate 2024-08-04 20:29:26
*/
@Service
public class SUpdateQuestionServiceImpl extends ServiceImpl<SUpdateQuestionMapper, SUpdateQuestion>
    implements SUpdateQuestionService{


    @Resource
    private SUpdateQuestionMapper sUpdateQuestionMapper;

    @Resource
    private SQuestionMapper sQuestionMapper;


    //老师提交申请修改题目
    @Override
    public Result submitApplication(SUpdateQuestion sUpdateQuestion) {
        int insert = sUpdateQuestionMapper.insert(sUpdateQuestion);
        return Result.OK(insert);
    }

    //根据老师的id查看对应的申请修改题目信息
    @Override
    public Result getUpdateQuestionById(SUpdateQuestionDto dto) {
        Page<SUpdateQuestion> page=new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<SUpdateQuestion> sUpdateQuestions = this.sUpdateQuestionMapper.getUpdateQuestionById(page, dto);
            return Result.OK(sUpdateQuestions);
    }


    //管理员通过提交的申请
    @Override
    public Result passApplication(Integer id) {
        SUpdateQuestion sUpdateQuestion = this.sUpdateQuestionMapper.selectById(id);
        SQuestion sQuestion = this.sQuestionMapper.selectById(sUpdateQuestion.getQId());
        sQuestion.setAnswerTxt(sUpdateQuestion.getAnswertxt());
        sQuestion.setQuestion(sUpdateQuestion.getStem());
        sUpdateQuestion.setStatus(1);
        this.sUpdateQuestionMapper.updateById(sUpdateQuestion);
        this.sQuestionMapper.updateById(sQuestion);
        return Result.OK("通过成功");
    }

    //管理员驳回提交的修改申请
    @Override
    public Result rejectApplication(Integer id,String cause) {
        SUpdateQuestion sUpdateQuestion = this.sUpdateQuestionMapper.selectById(id);
        sUpdateQuestion.setStatus(2);
        if (cause!=null){
            sUpdateQuestion.setBackCause(cause);
        }
        if (this.sUpdateQuestionMapper.updateById(sUpdateQuestion)!=0){
            return Result.OK("驳回成功");
        }
        return Result.OK("驳回失败");
    }


    //管理员查看所有的修改申请记录
    @Override
    public Result getAllUpdateQuestion(SUpdateQuestionDto dto) {
        Page<SUpdateQuestion> page=new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<SUpdateQuestion> sUpdateQuestions = this.sUpdateQuestionMapper.getAllUpdateQuestion(page, dto);
        return Result.OK(sUpdateQuestions);
    }


}




